# -*- coding=utf-8 -*-
# Author: Slp
# @Date : 2021-08-26
import os
import time

from selenium import webdriver
from time import sleep

from selenium.webdriver.common.by import By


class ScrollBar:

    def __init__(self):
        # self.self.driver = webdriver.Chrome()
        chrome_path = os.path.dirname(__file__) + '/webdriver/chromedriver.exe'
        self.driver = webdriver.Chrome(executable_path=chrome_path)
        self.driver.maximize_window()
        # self.driver.get("https://baidu.com")
        # self.driver.find_element_by_id('kw').send_keys("storm")
        # self.driver.find_element_by_id('su').click()
        # self.driver = webdriver.Firefox ()
        self.driver.get("https://test.jinyiyun.com/amo")
        self.driver.implicitly_wait(15)
        self.driver.delete_all_cookies()
        self.driver.add_cookie({'domain': '.test.jinyiyun.com',  # 此处xxx.com前，需要带点
                                'name': "FINANCE-CARPORT-TOKEN",
                                'value': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjEsIm1vYmlsZSI6IjEzMDU4MDE5MzAyIiwiaWF0IjoxNjI5ODc3NDUxLCJleHAiOjE2MzA0ODIyNTF9.Vg0ToomgW5rHPqVaXH-uOCph2S2Xv_63N10GSgEz9Xo",
                                'path': '/',
                                'expires': None})
        self.driver.get("https://test.jinyiyun.com/amo/project/list")
        self.driver.find_element_by_id('projectName').send_keys("智能回归")
        self.driver.find_element(By.XPATH,
                                 '//*[@id="root"]/section/section/div[1]/main/div/form/div/div[3]/div/div/div/span/button[1]/span').click()

        # self.js1 = "window.scrollTo(0, 10000)"  # 滑动滚动条到底部
        self.js1 = "document.getElementsByClassName('ant-layout')[0].getElementsByTagName('section')[0].scrollTop=10000"  # 滑动滚动条到底部
        # self.js1 = self.driver.find_element_by_class_name("ant-layout")[0]  # 滑动滚动条到底部
        self.js2 = "window.scrollTo(0,0)"  # 滑动到顶部
        self.js3 = "window.scrollTo(0,200)"  # 向下移动200像素
        self.js4 = "arguments[0].scrollIntoView();"  # 滑动滚动条到某个指定的元素
        self.js5 = "window.scrollTo(document.body.scrollWidth,0)"  # 移动到最右边
        self.js6 = "window.scrollTo(0,0)"  # 移动到最左边
        self.js7 = "window.scrollTo(200,0)"  # 向右移动200像素

    def scroll_to_bottom(self):

        # js = "return action=document.body.scrollHeight"
        new_height = self.driver.get_window_size()['height'] +300 # 窗口高度
        height = 0
        # new_height = self.driver.execute_script(js)
        print(new_height)
        while height < new_height:
            for i in range(height, new_height, 300):
                # self.driver.execute_script('window.scrollTo(0,{})'.format(i))
                self.driver.execute_script(
                    "document.getElementsByClassName('ant-layout')[0].getElementsByTagName('section')[0].scrollTo(0,{})".format(
                        i))
                time.sleep(0.5)
            height = new_height
            time.sleep(2)
            # new_height = self.driver.execute_script(js)

    def portrait_move(self):
        '''纵向滑滚动条'''
        sleep(2)
        self.driver.execute_script(self.js1)  # 执行js1，将滚动条滑到最下方
        # self.driver.execute_script("arguments[0].focus();", self.js1)  # 执行js1，将滚动条滑到最下方
        print("走完了，动了么？？？ ")
        sleep(2)  # 加等待时间，看效果
        self.driver.execute_script(self.js2)  # 执行js2，将滚动条滑到最上方
        sleep(2)  # 加等待时间，看效果
        self.driver.execute_script(self.js3)  # 执行js3，将滚动条向下滑到200像素
        sleep(2)  # 加等待时间，看效果
        self.driver.execute_script(self.js2)  # 执行js2，将滚动条滑到最上方
        sleep(2)
        ele = self.driver.find_element_by_id('con-ar')  # 定位一个元素
        self.driver.execute_script(self.js4, ele)  # 滑动到上面定位的元素的地方
        sleep(2)
        self.driver.quit()

    def transverse_move(self):
        '''横向移动滚动条'''
        self.driver.set_window_size(500, 500)  # 缩小浏览器窗口，使之出现横向滚动条
        sleep(2)
        self.driver.execute_script(self.js5)  # 移动到最右边
        sleep(2)
        self.driver.execute_script(self.js6)  # 移动到最左边
        sleep(2)
        self.driver.execute_script(self.js7)  # 向右移动200像素
        sleep(2)
        self.driver.quit()

    def iframe_move(self):
        '''操作内嵌滚动条'''
        # self.driver.get ("http://sahitest.com/demo/iframesTest.htm")
        self.driver.switch_to.frame(0)  # 1代表id=1的frame（框架）
        sleep(2)
        self.driver.execute_script(self.js5)  # 向下移动200像素sleep(2)
        sleep(2)
        self.driver.quit()


if __name__ == '__main__':
    a = ScrollBar()
    # a.portrait_move()
    a.scroll_to_bottom()
